package service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.TbItemMapper;
import org.springframework.stereotype.Service;
import pojo.*;
import service.ItemService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private TbItemMapper tbItemMapper;

    /**
     * 返回商品列表
     *
     * @param page
     * @param rows
     * @return
     */
    @Override
    public EasyUiDataGridResult getTbItemList(int page, int rows) {
        TbItemExample tbItemExample = new TbItemExample();
        //分页处理
        PageHelper.startPage(page,rows);
        //查询商品列表
        List<TbItem> list = tbItemMapper.selectByExample(tbItemExample);
        //创建pageInfo对象
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        //创建EasyUiDataGridResult返回对象
        EasyUiDataGridResult easyUiDataGridResult = new EasyUiDataGridResult();
        easyUiDataGridResult.setRows(list);
        easyUiDataGridResult.setTotal(pageInfo.getTotal());
        return easyUiDataGridResult;
    }

    /**
     * 商品添加
     *
     * @param tbItem
     * @return
     */
    public TaotaoResult createItem(TbItem tbItem){
        //tbItem补全
        //生成商品id
        Long id = IDUtils.genItemId();
        tbItem.setId(id);
        tbItem.setStatus((byte) 1);
        tbItem.setCreated(new Date());
        tbItem.setUpdated(new Date());
        //插入到数据库
        tbItemMapper.insert(tbItem);
        return TaotaoResult.ok();
    }
}
